﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PDF = iTextSharp.text.pdf;
using IT = iTextSharp.text;
using System.IO;

namespace DealPDFs
{
    /// <summary>
    /// 处理奇数页PDF文档
    /// </summary>
    internal class Program
    {
        static void Main(string[] args)
        {
            try
            {
                foreach(string a in args)
                {
                    Task.Run(() =>
                        DealPDF(a));
                }


            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadKey();
        }

        /// <summary>
        /// 将PDF文档补齐偶数页
        /// </summary>
        /// <param name="filePath">文件</param>
        private static void DealPDF(string filePath)
        {
            Console.WriteLine("正在处理文件：" + filePath);

            //读取PDF文件信息
            PDF.PdfReader PDFR = new PDF.PdfReader(filePath);
            //获取PDF页码总数
            int curNOP = PDFR.NumberOfPages;
            Console.WriteLine("\r\n读取完毕，该文件总页数为：" + curNOP);

            //补全奇数页文档
            if (curNOP%2!=0)
            {
                Console.BackgroundColor = ConsoleColor.DarkGreen;
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("正在为奇数页文件补齐背面空白页");

                using (MemoryStream myMemoryStream = new MemoryStream())
                {
                    //建立A4空文档
                    IT.Document doc = new IT.Document(IT.PageSize.A4/*.Rotate()*/);

                    //生产PDFWriter实例
                    PDF.PdfWriter PDFW = PDF.PdfWriter.GetInstance(doc, myMemoryStream);

                    //打开文档
                    doc.Open();

                    //循环拷贝原文件到流
                    for (int j = 1; j <= curNOP; j++)
                    {
                        //获取Reader的pdf页的打印方向
                        var re = PDFR.GetPageSize(PDFR.GetPageN(j));
                        //设置合并pdf的打印方向
                        doc.SetPageSize(re);
                        //文档插入新页
                        doc.NewPage();
                        //使用writer获取要写入的页
                        PDF.PdfImportedPage newPage = PDFW.GetImportedPage(PDFR, j);

                        //写入byte流
                        PDFW.DirectContent.AddTemplate(newPage, 0, 0);
                    }

                    //末尾增加空白页
                    doc.NewPage();
                    //写入空格段
                    doc.Add(new IT.Paragraph(" "));

                    //关闭文档
                    doc.Close();

                    //准备流
                    byte[] content = myMemoryStream.ToArray();

                    //将流写入文件
                    using (FileStream fs = File.Create(filePath.Replace(".pdf",".new.pdf")))
                    {
                        fs.Write(content, 0, (int)content.Length);
                    }

                    //关闭writer
                    PDFW.Close();
                }
                //关闭reader
                PDFR.Close();

                File.Replace(filePath.Replace(".pdf", ".new.pdf"), filePath, null);
            }
        }
    }
}
